import ReplyItem from './ReplyItem';
import CommunicationRichText from './CommunicationRichText';
import isFunction from 'lodash/isFunction';
import {Pagination} from 'antd';
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
class ReplyList extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      edit: false,
      replyId: '',
      receiveUser:'',//被回复人
      pageNum: 1,
      pageSize: 5,
      placeholder: ''
    };
  }
  render() {
    const {list, isCanAnonymous, canReply, requestid,  hasSubmitRight = false} = this.props;
    let {edit, pageNum, pageSize} = this.state, total = list.length;
    let showList = [];
    let endNum = pageNum * pageSize;
    if (endNum > total) {
      endNum = total;
    }
    for (let i = (pageNum - 1) * pageSize; i < endNum; i++) {
      showList.push(list[i]);
    }
    return (
      <div className="wea-cowork-comment">
        {showList.map((item,index) => {
          return <ReplyItem ecId={`${this && this.props && this.props.ecId || ''}_ReplyItem@tn1fwg@${index}`} operate={this.props.operate} changePageNum={this.changePageNum} {...item} reply={this.reply} hasSubmitRight={hasSubmitRight}/>
        })}
        {total > pageSize &&
          (
            <div className="small-page">
              <Pagination ecId={`${this && this.props && this.props.ecId || ''}_Pagination@l0j16q`} size="small" total={total} pageSize={pageSize} onChange={this.changePage} current={pageNum}/>
              <div className="clear-both"/>
            </div>
          )}
        {edit && <CommunicationRichText ecId={`${this && this.props && this.props.ecId || ''}_CommunicationRichText@dmgone`} requestid={requestid} hasSubmit={true} ref="richText" submit={this.submit} cancel={this.cancel}
                                appStatus={this.props.appStatus}  isCanAnonymous={isCanAnonymous} editType="comment" placeholder={this.state.placeholder}/>}
      </div>
    );
  }
  

  changePageNum = () => { //子回复删除,底部分页器中pageNum变化
    const { list } = this.props;
    const { pageSize } = this.state;
    let total = list.length;
    if(total - 1 <= pageSize ) {
      this.setState({ pageNum: 1});
    } else if( (total - 1 > pageSize) && (list.length - 1 % 5 == 0)) {
      this.setState( { pageNum: --pageNum});
    }
  }


  reply = (id, receiveUser, receiveUserName, resiveUserType) => {
    let passValue = getLabel('385534', "回复")  + receiveUserName;
    this.setState({
      edit: true,
      replyId: id,
      receiveUser : receiveUser,
      resiveUserType: resiveUserType,
      placeholder: passValue,
    });
  }
  
  submit = (params, callback) => {
    let saveParams = {...params, replyType: 'comment'};
    saveParams.replayid = this.state.replyId;
    saveParams.receiveUser = this.state.receiveUser;
    saveParams.resiveUserType = this.state.resiveUserType;
    saveParams.contentId = this.props.contentId;
    isFunction(this.props.submit) && this.props.submit(saveParams, this.cancel.bind(this, callback));
  }

  cancel = (callback) => {
    this.setState({
      edit: false,
      replyId: '',
      receiverUser:''
    },()=>{
      this.refs.richText && this.refs.richText.wrappedInstance && this.refs.richText.wrappedInstance.clear();
    });
    isFunction(callback) && callback();
  }
  
  changePage = (pageNum) => {
    this.setState({pageNum});
  }
}

export default ReplyList;